package com.datang.petstore.service.impl;
/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-10-02 15:09
 * @notify 用户账号相关接口实现
 * @version 1.0
 */


import com.datang.petstore.entity.UserInfo;
import com.datang.petstore.entity.UserInfoExample;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.UserInfoMapper;
import com.datang.petstore.mapper.UserInfoCustomMapper;
import com.datang.petstore.model.SummaryOpenSourceResponse;
import com.datang.petstore.model.SummaryUserAgeResponse;
import com.datang.petstore.model.SummaryUserSexResponse;
import com.datang.petstore.model.getUserLoginSummaryResponse;
import com.datang.petstore.resultpage.Result;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.UserService;
import com.datang.petstore.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserInfoCustomMapper userInfoCustomMapper;


    /*
     * @author 顶风少年
     * @date 2019/10/8 13:48
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param loginUserRequest
     * @result com.datang.petstore.utils.Result
     * @notify 查询用户是否存在,如果存在返回,不存在创建并返回
     * version 1.0
     */
    @Transactional
    public UserInfo loginUserInfo(String openId, String openSource) {
        //当前数据库支持的三方类型
        List<Integer> sourceFlag = new ArrayList<>();
        sourceFlag.add(0);
        sourceFlag.add(1);
        sourceFlag.add(2);
        sourceFlag.add(3);


        if (!sourceFlag.contains(Integer.parseInt(openSource))) {
            throw new AcceptResultCodeException(ResultCode.error501);
        }


        //根据三方id和三方来源查询用户
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        criteria.andOpenSourceEqualTo(openSource);
        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);

        //如果查询不到用户创建一个
        if (userInfos.size() == 0) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(StringUtils.getId());
            userInfo.setOpenId(openId);
            userInfo.setOpenSource(openSource);
            userInfo.setRegisterDate(new Date());
            userInfo.setForbid("0");
            userInfo.setCreditValue(0);
            userInfoMapper.insertSelective(userInfo);
            return userInfo;
        } else {
            UserInfo userInfo = userInfos.get(0);
            if (userInfo.getForbid().equals("1")) {
                throw new AcceptResultCodeException(ResultCode.error503);
            } else {
                return userInfo;
            }
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/10/22 15:57
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result com.datang.petstore.utils.Result
     * @notify 根据用户id获取用户信息
     * version 1.0
     */
    @Cacheable(value = "getUserInfo", key = "#userId")
    public UserInfo getUserInfo(String userId) {
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if (userInfos.size() != 1) {
            throw new AcceptResultCodeException(ResultCode.error502);
        }
        return userInfos.get(0);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/15 6:28
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.petstore.model.SummaryUserSexResponse>
     * @notify 用户性别汇总
     * version 1.0
     */
    public List<SummaryUserSexResponse> getSummaryUserSex() {
        List<SummaryUserSexResponse> summaryUserSex = userInfoCustomMapper.getSummaryUserSex();
        return summaryUserSex;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 6:28
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.petstore.model.SummaryUserAgeResponse>
     * @notify 用户年龄汇总
     * version 1.0
     */
    public List<SummaryUserAgeResponse> getSummaryUserAge() {
        List<SummaryUserAgeResponse> summaryUserAge = userInfoCustomMapper.getSummaryUserAge();
        return summaryUserAge;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 6:29
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.petstore.model.SummaryOpenSourceResponse>
     * @notify 用户年龄汇总
     * version 1.0
     */
    public List<SummaryOpenSourceResponse> getSummaryOpenSource() {
        List<SummaryOpenSourceResponse> summaryOpenSource = userInfoCustomMapper.getSummaryOpenSource();
        return summaryOpenSource;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 7:02
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.petstore.model.getUserLoginSummaryResponse>
     * @notify 用户七日汇总
     * version 1.0
     */
    public List<getUserLoginSummaryResponse> getUserLoginSummary() {
        List<getUserLoginSummaryResponse> userLoginSummary = userInfoCustomMapper.getUserLoginSummary();
        return userLoginSummary;
    }


    /*
     * @author 顶风少年
     * @date 2019/11/18 11:22
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param page
     * @result java.util.List<com.datang.petstore.entity.UserInfo>
     * @notify 分页获取用户列表
     * version 1.0
     */
    public PageInfo getUserInfoList(PageInfo page) {
        System.out.println(page.getPageNum());
        System.out.println(page.getPageSize());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<UserInfo> userInfos = userInfoMapper.selectByExample(null);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        return pageInfo;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/18 16:39
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userInfo
     * @result void
     * @notify 更改用户信息
     * version 1.0
     */
    public void updateUserInfo(UserInfo userInfo) {
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setForbid(userInfo.getForbid());
        userInfo1.setCreditValue(userInfo.getCreditValue());
        userInfo1.setId(userInfo.getId());
        userInfoMapper.updateByPrimaryKeySelective(userInfo1);
    }
}
